<?php
namespace app\shop\controller;
use app\AdminRest;
use app\shop\model\Article;
use app\shop\model\Banner;
use app\shop\model\Cap;
use app\shop\model\Date;
use app\shop\model\MsgConfig;
use app\shop\model\OrderAddress;
use app\shop\model\OrderGoods;
use app\shop\model\RefundOrder;
use app\shop\model\RefundOrderGoods;
use app\shop\model\Wallet;
use think\App;
use app\shop\model\Order as Model;
use think\facade\Db;


class AdminOrder extends AdminRest
{


    protected $model;

    protected $order_goods_model;

    protected $refund_order_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->order_goods_model = new OrderGoods();

        $this->refund_order_model = new RefundOrder();

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:43
     * @功能说明:列表
     */
    public function orderList(){

        //超时自动取消订单
        $this->model->autoCancelOrder($this->_uniacid);

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];
        //时间搜素
        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];
        }
        //商品名字搜索
        if(!empty($input['goods_name'])){

            $dis[] = ['c.goods_name','like','%'.$input['goods_name'].'%'];
        }
        //手机号搜索
        if(!empty($input['mobile'])){

            $dis[] = ['d.mobile','like','%'.$input['mobile'].'%'];
        }
        //订单状态搜索
        if(!empty($input['pay_type'])){

            $dis[] = ['a.pay_type','=',$input['pay_type']];
        }

        if(!empty($input['order_code'])){

            $dis[] = ['a.order_code','like','%'.$input['order_code'].'%'];
        }

        if(!empty($input['send_type'])){

            $dis[] = ['a.send_type','=',$input['send_type']];
        }

        $data = $this->model->adminDataList($dis,$input['limit']);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-04 09:39
     * @功能说明:修改订单
     */
    public function orderGoodsSend(){

        $input = $this->_input;

        $update = [
            //快递公司
            'express_company' => $input['express_company'],
            //快递电话
            'express_mobile'  => $input['express_mobile'],
            //快递单号
            'express_code'    => $input['express_code'],

            'send_time'       => time(),

            'pay_type'        => 3,
        ];

        $res = $this->model->dataUpdate(['id'=>$input['id']],$update);

        return $this->success($res);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-04 09:52
     * @功能说明:核销订单
     */
    public function hxOrder(){

        $input = $this->_input;

        $order = $this->model->dataInfo(['id'=>$input['id']]);

        if(empty($order)){

            $this->errorMsg('订单未找到');
        }
        //自提
        if($order['send_type']==1&&$order['pay_type']!=2){

            $this->errorMsg('订单状态错误');
        }
        //快递
        if($order['send_type']==2&&$order['pay_type']!=3){

            $this->errorMsg('订单状态错误');
        }

        $refund_model = new RefundOrder();
        //判断有无申请中的退款订单
        $refund_order = $refund_model->dataInfo(['order_id'=>$order['id'],'status'=>1]);

        if(!empty($refund_order)){

            $this->errorMsg('该订单正在申请退款，请先处理再核销');

        }

        $res = $this->model->hxOrder($order);

        return $this->success($res);
    }

    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:58
     * @功能说明:订单详情
     */
    public function orderInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-19 17:50
     * @功能说明:退款订单详情
     */
    public function refundOrderInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->refund_order_model->dataInfo($dis);

        $data['pay_order_code'] = $this->model->where(['id'=>$data['order_id']])->value('order_code');

        $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);

        $data['refund_time'] = date('Y-m-d H:i:s',$data['refund_time']);

        return $this->success($data);

    }
    /**
     * @author chenniang
     * @DataTime: 2021-03-17 17:44
     * @功能说明:退款订单列表
     */
    public function refundOrderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        //商品名字搜索
        if(!empty($input['goods_name'])){

            $dis[] = ['c.goods_name','like','%'.$input['goods_name'].'%'];
        }
        //订单状态搜索
        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];

        }else{

            $dis[] = ['a.status','>',-1];

        }

        $where = [];

        if(!empty($input['order_code'])){

            $where[] = ['a.order_code','like','%'.$input['order_code'].'%'];

            $where[] = ['d.order_code','like','%'.$input['order_code'].'%'];
        }
        $data = $this->refund_order_model->adminDataList($dis,$input['limit'],$where);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-18 09:21
     * @功能说明:拒绝退款
     */
    public function noPassRefund(){

        $input = $this->_input;

        $res = $this->refund_order_model->noPassRefund($input['id']);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**\
     * @author chenniang
     * @DataTime: 2021-03-18 09:28
     * @功能说明:同意退款
     */
    public function passRefund(){

        $input = $this->_input;

        $res = $this->refund_order_model->passOrder($input['id'],$input['price'],$this->payConfig(),0,$input['text']);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-18 13:37
     * @功能说明:数据统计
     */
    public function dateCount(){

        $input = $this->_param;

        $cap_id = $input['cap_id'];

        $date_model   = new Date();

        $wallet_model = new Wallet();

        $cap_model    = new Cap();

        $date_model->dataInit($this->_uniacid);

        $dis[] = ['uniacid','=',$this->_uniacid];
        //时间搜素
        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time = $input['end_time'];

            $dis[] = ['date_str','between',"$start_time,$end_time"];
        }

        $date_list = $date_model->dataList($dis,$input['limit']);
        //店铺名字
        $date_list['store_name'] = $cap_model->where(['id'=>$cap_id])->value('store_name');
        //开始时间结束时间
        if(!empty($start_time)){

            $date_list['start_time'] = $start_time;

            $date_list['end_time']   = $end_time;

        }else{

            $date_list['start_time'] = $date_model->where(['uniacid'=>$this->_uniacid])->min('date_str');

            $date_list['end_time']   = $date_model->where(['uniacid'=>$this->_uniacid])->max('date_str');

        }

        if(!empty($date_list['data'])){

            foreach ($date_list['data'] as &$v){
                //订单金额
                $v['order_price']  = $this->model->datePrice($v['date_str'],$this->_uniacid,$cap_id);
                //退款金额
                $v['refund_price'] = $this->refund_order_model->datePrice($v['date_str'],$this->_uniacid,$cap_id);
                //提现金额
                $v['wallet_price'] = $wallet_model->datePrice($v['date_str'],$this->_uniacid,$cap_id);

            }

        }

        return $this->success($date_list);

    }









}
